home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / PRUXENV.C < prev    next >
C/C++ Source or Header  |  1992-03-25  |  6KB  |  193 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/src/microcode/RCS/pruxenv.c,v 1.6 1992/03/26 03:56:12 cph Exp $
  4.  
  5. Copyright (c) 1990-92 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* Unix-specific process-environment primitives. */
  36.  
  37. #include "scheme.h"
  38. #include "prims.h"
  39. #include "ux.h"
  40.  
  41. #ifdef HAVE_SOCKETS
  42. #include "uxsock.h"
  43. #include <sys/socket.h>
  44. #include <netinet/in.h>
  45. #include <netdb.h>
  46. #endif
  47.  
  48. extern char ** environ;
  49.  
  50. DEFINE_PRIMITIVE ("CURRENT-FILE-TIME", Prim_current_file_time, 0, 0,
  51.   "Return the current file system time stamp.\n\
  52. This is an integer whose units are in seconds.")
  53. {
  54.   PRIMITIVE_HEADER (0);
  55.   PRIMITIVE_RETURN (long_to_integer (UX_time (0)));
  56. }
  57.  
  58. DEFINE_PRIMITIVE ("FILE-TIME->STRING", Prim_file_time_to_string, 1, 1,
  59.   "Convert a file system time stamp into a date/time string.")
  60. {
  61.   PRIMITIVE_HEADER (1);
  62.   CHECK_ARG (1, INTEGER_P);
  63.   {
  64.     time_t clock = (arg_integer (1));
  65.     char * time_string = (UX_ctime (&clock));
  66.     (time_string[24]) = '\0';
  67.     PRIMITIVE_RETURN (char_pointer_to_string ((unsigned char *) time_string));
  68.   }
  69. }
  70.  
  71. DEFINE_PRIMITIVE ("GET-USER-HOME-DIRECTORY", Prim_get_user_home_directory, 1, 1,
  72.   "Return the file name of a given user's home directory.\n\
  73. The user name argument must be a string.\n\
  74. If no such user is known, #F is returned.")
  75. {
  76.   PRIMITIVE_HEADER (1);
  77.   {
  78.     struct passwd * entry = (UX_getpwnam (STRING_ARG (1)));
  79.     PRIMITIVE_RETURN
  80.       ((entry == 0) ? SHARP_F
  81.        : (char_pointer_to_string ((unsigned char *) (entry -> pw_dir))));
  82.   }
  83. }
  84.  
  85. DEFINE_PRIMITIVE ("UID->STRING", Prim_uid_to_string, 1, 1,
  86.   "Return the user name corresponding to UID.\n\
  87. If the argument is not a known user ID, #F is returned.")
  88. {
  89.   PRIMITIVE_HEADER (1);
  90.   {
  91.     struct passwd * entry = (UX_getpwuid (arg_nonnegative_integer (1)));
  92.     PRIMITIVE_RETURN
  93.       ((entry == 0) ? SHARP_F
  94.        : (char_pointer_to_string ((unsigned char *) (entry -> pw_name))));
  95.   }
  96. }
  97.  
  98. DEFINE_PRIMITIVE ("GID->STRING", Prim_gid_to_string, 1, 1,
  99.   "Return the group name corresponding to GID.\n\
  100. If the argument is not a known group ID, #F is returned.")
  101. {
  102.   PRIMITIVE_HEADER (1);
  103.   {
  104.     struct group * entry = (UX_getgrgid (arg_nonnegative_integer (1)));
  105.     PRIMITIVE_RETURN
  106.       ((entry == 0) ? SHARP_F
  107.        : (char_pointer_to_string ((unsigned char *) (entry -> gr_name))));
  108.   }
  109. }
  110.  
  111. DEFINE_PRIMITIVE ("CURRENT-UID", Prim_current_uid, 0, 0,
  112.   "Return Scheme's effective UID.")
  113. {
  114.   PRIMITIVE_HEADER (0);
  115.   PRIMITIVE_RETURN (long_to_integer (UX_geteuid ()));
  116. }
  117.  
  118. DEFINE_PRIMITIVE ("CURRENT-GID", Prim_current_gid, 0, 0,
  119.   "Return Scheme's effective GID.")
  120. {
  121.   PRIMITIVE_HEADER (0);
  122.   PRIMITIVE_RETURN (long_to_integer (UX_getegid ()));
  123. }
  124.  
  125. DEFINE_PRIMITIVE ("SYSTEM", Prim_system, 1, 1,
  126.   "Invoke sh (the Bourne shell) on the string argument.\n\
  127. Wait until the shell terminates, returning its exit status as an integer.")
  128. {
  129.   PRIMITIVE_HEADER (1);
  130.   PRIMITIVE_RETURN (long_to_integer (UX_system (STRING_ARG (1))));
  131. }
  132.  
  133. DEFINE_PRIMITIVE ("UNIX-ENVIRONMENT", Prim_unix_environment_alist, 0, 0,
  134.   "Copy the unix environment and return it as a vector of strings.")
  135. {
  136.   PRIMITIVE_HEADER (0);
  137.   {
  138.     char ** scan = environ;
  139.     char ** end = scan;
  140.     while ((*end++) != 0);
  141.     end -= 1;
  142.     {
  143.       SCHEME_OBJECT result =
  144.     (allocate_marked_vector (TC_VECTOR, (end - scan), 1));
  145.       SCHEME_OBJECT * scan_result = (VECTOR_LOC (result, 0));
  146.       while (scan < end)
  147.     (*scan_result++) =
  148.       (char_pointer_to_string ((unsigned char *) (*scan++)));
  149.       PRIMITIVE_RETURN (result);
  150.     }
  151.   }
  152. }
  153.  
  154. #define HOSTNAMESIZE 1024
  155.  
  156. DEFINE_PRIMITIVE ("FULL-HOSTNAME", Prim_full_hostname, 0, 0,
  157.   "Returns the full hostname (including domain if available) as a string.")
  158. {
  159.   PRIMITIVE_HEADER (0);
  160.   {
  161.     char this_host_name[HOSTNAMESIZE];
  162. #ifdef HAVE_SOCKETS
  163.     struct hostent * EXFUN (gethostbyname, (const char *));
  164.     struct hostent *this_host_entry;
  165. #endif
  166.     STD_VOID_SYSTEM_CALL (syscall_gethostname,
  167.               UX_gethostname (this_host_name, HOSTNAMESIZE));
  168.  
  169. #ifdef HAVE_SOCKETS
  170.     this_host_entry = gethostbyname (this_host_name);
  171.     PRIMITIVE_RETURN
  172.       (char_pointer_to_string ((unsigned char *) (this_host_entry->h_name)));
  173. #else
  174.     PRIMITIVE_RETURN
  175.       (char_pointer_to_string ((unsigned char *) this_host_name));
  176. #endif
  177.   }
  178. }
  179.  
  180. DEFINE_PRIMITIVE ("HOSTNAME", Prim_hostname, 0, 0,
  181.   "Returns the hostname of the machine as a string.")
  182. {
  183.   PRIMITIVE_HEADER (0);
  184.   {
  185.     char this_host_name[HOSTNAMESIZE];
  186.  
  187.     STD_VOID_SYSTEM_CALL (syscall_gethostname,
  188.               UX_gethostname (this_host_name, HOSTNAMESIZE));
  189.     PRIMITIVE_RETURN
  190.       (char_pointer_to_string ((unsigned char *) this_host_name));
  191.   }
  192. }
  193.